package com.why.simpleboot.core.configration;

import com.why.simpleboot.core.resource.PropertiesResourceLoader;
import com.why.simpleboot.core.resource.ResourceLoader;
import com.why.simpleboot.core.resource.YamlResourceLoader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: why-simpleboot-framework
 * @description:
 * @author: @why
 * @create: 2021-05-27 20:08
 **/
public class SimpleBootConfiguration {
    private static final Logger log = LoggerFactory.getLogger(SimpleBootConfiguration.class);
    /**
     * 仅仅会读取名字叫 application 的配置文件
     */
    private final String[] CONFIG_NAMES = {"application.properties", "application.yaml"};
    private final String PROPERTIES_FILE_EXTENSION = ".properties";
    private final String YAML_FILE_EXTENSION = ".yaml";
    private final String YML_FILE_EXTENSION = ".yml";
    private final HashMap<String, String> CONFIGURATION = new HashMap<>();


    public Integer getInt(String id) {
        if (CONFIGURATION.containsKey(id)) {
            return Integer.valueOf(CONFIGURATION.get(id));
        }

        return null;
    }


    public String getString(String id) {
        if (CONFIGURATION.containsKey(id)) {
            return String.valueOf(CONFIGURATION.get(id));
        }

        return null;
    }


    public Boolean getBoolean(String id) {
        if (CONFIGURATION.containsKey(id)) {
            return Boolean.parseBoolean(CONFIGURATION.get(id));
        }
        return null;
    }


    /**
     * 加载指定目录中的资源
     *
     * @param resourcePaths
     */
    public void loadResources(List<Path> resourcePaths) {
        try {
            for (Path resourcePath : resourcePaths) {
                String fileName = resourcePath.getFileName().toString();
                //加载properties配置文件
                if (fileName.endsWith(PROPERTIES_FILE_EXTENSION)) {
                    ResourceLoader resourceLoader = new PropertiesResourceLoader();
                    CONFIGURATION.putAll(resourceLoader.loadResource(resourcePath));
                    //加载yaml配置文件
                } else if (fileName.endsWith(YML_FILE_EXTENSION) || fileName.endsWith(YAML_FILE_EXTENSION)) {
                    ResourceLoader resourceLoader = new YamlResourceLoader();
                    Map<String, String> map = resourceLoader.loadResource(resourcePath);
                    if (map != null) {
                        CONFIGURATION.putAll(map);
                    }
                }
            }

        } catch (IOException ex) {
            log.error("load resources defeat.",ex);

        }
    }

    public String getPropertiesFileExtension() {
        return PROPERTIES_FILE_EXTENSION;
    }

    public String getYamlFileExtension() {
        return YAML_FILE_EXTENSION;
    }

    public String getYmlFileExtension() {
        return YML_FILE_EXTENSION;
    }

    public String[] getConfigNames() {
        return CONFIG_NAMES;
    }
}
